﻿using Aop.Api.Util;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Return_url : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        /* 实际验证过程建议商户添加以下校验。
        1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号，
        2、判断total_amount是否确实为该订单的实际金额（即商户订单创建时的金额），
        3、校验通知中的seller_id（或者seller_email) 是否为out_trade_no这笔单据的对应的操作方（有的时候，一个商户可能有多个seller_id/seller_email）
        4、验证app_id是否为该商户本身。
        */
        Dictionary<string, string> sArray = GetRequestGet();
        if (sArray.Count != 0)
        {
            bool flag = AlipaySignature.RSACheckV1(sArray, config.alipay_public_key, config.charset, config.sign_type, false);
            if (flag)
            {
                Response.Write("同步验证通过");
            }
            else
            {
                Response.Write("同步验证失败");
            }
        }
    }

    public Dictionary<string, string> GetRequestGet()
    {
        int i = 0;
        Dictionary<string, string> sArray = new Dictionary<string, string>();
        NameValueCollection coll;
        //coll = Request.Form;
        coll = Request.QueryString;
        String[] requestItem = coll.AllKeys;
        for (i = 0; i < requestItem.Length; i++)
        {
            sArray.Add(requestItem[i], Request.QueryString[requestItem[i]]);
        }
        return sArray;

    }

    public class config
    {
        public config()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        // 应用ID,您的APPID
        public static string app_id = "2016102300747987";

        // 支付宝网关
        public static string gatewayUrl = "https://openapi.alipay.com/gateway.do";

        // 商户私钥，您的原始格式RSA私钥
        public static string private_key = "MIIEogIBAAKCAQEAhcupiXtmGU/FRXWzJhwOFRv3UwX77DnbtEbReF5bdeDrTkchSujkiQ4+ugGrVV1pt4x2NJUMHHwSD7zqWLHJeW/ZFub4JZSOJCEPUBSRnLXWg6jIbuLw0xIqGIvYm+S8GJTArH7x4MqNK0nisIYp6AR/yqdqosNFzeK+M3lQlPTAjTSDzIZyEaTmNUVO57mMZ2a+K0JqL1hD9tPq7lSb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zWpr6VYKgzzvp/wc8WZznmz7qtYsYD8LjVhbxExLqByV988eSPHZLlg5jpWQ9litOspWQzFptBF9a7BkZ1+BMfZA9zX5pGlgiGEI8Rkpjr7CIzBsMpv6+glAoGAGDTX8HgPLVMrT4gXieMwOpC1cGPiQhlDOBNOd7caDTQu3IX8Hd2vGVUskJbiQAHvAEq2g+Ngj+SMh2GnyOpLBCy19YxtcHyPHYAe9coxFwlJsE52ezsWAM4A06YwRShl8iUr/ogiNgpcXqFL3OiEUc4M3727rI4+ObnsckswzTg=";

        // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
        public static string alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmP4mMFQnvqOcnt2si64gZVgDSR6SUBtaAA6NVxH+rV4CAYSx+exa4R0yPoZGeKscAvJu3WcmLeIoReW7eF1mlZCHD0YRI1KhIvK/tfLrJqZpIaAp+D3Py3zdqQkLck51wVp+HS3wEOJfC8akDvGOfSL/WCUVeRiSAj+2QusRYyuqDTEtsfwq3aB+tlfe+W00yAOl+MDfeGNYmpfx8deRisjJieWLJJmdnPBaf3nAtOtD7texmpkDh/XyGj4CZ9eQqRy3jqoklINRl3u4yb+NGj9XJAg4pGmBzGXi2wqiyqtlDxv0hml1Vh14zNI8IcQeE/f7ve0yl9tOQEEd0fP8VQIDAQAB";

        // 签名方式
        public static string sign_type = "RSA2";

        // 编码格式
        public static string charset = "UTF-8";
    }
}